<?php

/**
* alltosun.com 文件widget attachment.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 钱有明 (qianym@alltosun.com) $
* $Date: 2014-6-27 下午12:11:53 $
* $Id$
*/

class attachment_widget
{
    private $per_page = 10;

    public function get_res_attachment_list($params)
    {
        $page_no = Request::getParam('page_no', 0);

        $filter = $relation_filter = array();
        $empty_attachment = 0;
        if (isset($params['res_name']) && $params['res_name']) {
            $relation_filter['res_name'] = $params['res_name'];
        }
        if (isset($params['res_id']) && $params['res_id']) {
            $relation_filter['res_id'] = $params['res_id'];
        }

        if (!$relation_filter) {
            $empty_attachment = 1;
        }

        $attachment_ids = _model('attachment_relation')->getFields('attachment_id', $relation_filter);
        if (!$attachment_ids) {
            $empty_attachment = 1;
        } else {
            $filter['id'] = $attachment_ids;
        }
        $filter['status'] = 1;

        $per_page = $this->per_page;
        if (isset($params['per_page']) && $params['per_page']) {
            $per_page = $params['per_page'];
        }

        $attachment_list = array();
        if (!$empty_attachment) {
            $attachment_total = _model('attachment')->getTotal($filter);
            if ($attachment_total) {
                $pager = new Pager($per_page);
                if ($pager->generate($attachment_total)) {
                    Response::assign('pager', $pager);
                }

                $attachment_list = _model('attachment')->getList($filter, ' ORDER BY `id` ASC '.$pager->getLimit($page_no));
            }
        }

        Response::assign('attachment_list', $attachment_list);

        return $attachment_list;
    }
}
?>